/**
 * 
 */
package com.base.myproject.client.busi.BO;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * @author zgw
 *2009-8-29 下午07:22:43
 * javazgw@gmail.com
 */
public class BusiObject extends BaseObject implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8056004305082156617L;
	protected StoreInfo storeinfo;
	protected java.util.Map<String, Object> keyoldvalue = new java.util.HashMap<String, Object>();
	public BusiObject() {
		
	}
	/**
	 * 
	 */
	public BusiObject(StoreInfo storeinfo) {
		this.storeinfo = storeinfo;
	}

	/**
	 * @param properties
	 */
	public BusiObject(Map<String, Object> properties) {
		super(properties);
		
		
	}
/*	public boolean equals(Object bo)
	{
		return true;
	}
	public String toString()
	{
		return "";
	}*/
	/**
	 * 插入的sql
	 */
	public String insert()
	{
		DateTimeFormat activeFormat = DateTimeFormat
		.getFormat("yyyy-M-d H:mm:ss");
		StringBuffer insertsql =new StringBuffer();
		insertsql.append("insert into "+storeinfo.getTablename());
		insertsql.append(" (");
		Class c ;
		Iterator<String> iterator = this.getPropertyNames().iterator();
		StringBuffer sbvalue = new StringBuffer();
		while(iterator.hasNext())
		{
			
			String key = iterator.next();
			
			
			if(get(key) instanceof ModelData)
			{
				Object o=((ModelData)get(key)).get("code");
				if(o==null)
				{
					insertsql.append(""+key+",");
					sbvalue.append("null,");
				}
				else
				{
					insertsql.append(""+key+",");
					sbvalue.append("'"+o+"',");
				}
			}
			else if(get(key)==null)
			{
				insertsql.append(""+key+",");
				sbvalue.append(" null,");
			}
			else
			{
				c = get(key).getClass();
				//日期类型
				 if(c.equals(Date.class))
				{
				
							if(get(key)!=null)
							{
								insertsql.append(""+key+",");
								sbvalue.append("'"+activeFormat.format((Date)get(key))+"',");
								//updatesql.append(""+key+"='"+activeFormat.format((Date)get(key))+"',");
							}
							
							else
							{
								insertsql.append(""+key+",");
								sbvalue.append("null,");
							}
					
				}
				 //money 类型
				 else if(c.equals(Double.class))
				 {
					 if(get(key)!=null)
						{
							insertsql.append(""+key+",");
							sbvalue.append(""+get(key)+",");
							//updatesql.append(""+key+"='"+activeFormat.format((Date)get(key))+"',");
						}
						
						else
						{
							insertsql.append(""+key+",");
							sbvalue.append("null,");
						}
				 }
				
				else
				{
					insertsql.append(""+key+",");
					sbvalue.append("'"+get(key)+"',");
				}
			}
		}
		insertsql = insertsql.replace(insertsql.length()-1, insertsql.length(), "");
		sbvalue = sbvalue.replace(sbvalue.length()-1, sbvalue.length(), "");
		insertsql.append(" ) values(");
		insertsql.append(sbvalue+")");
//		for(int i=0;i<this.getPropertyNames().size();i++)
//		{
//			insertsql +=""+this.getPropertyNames().iterator();
//		}
	//	GWT.log(insertsql.toString(), null);
		return new String (insertsql);
	}
	/**
	 * 更新sql
	 * 注意程序处理了icode，
	 */
	//TODO  以后需要完成的工作，不要将没有修改的字段也提交到服务器更新
	public String update()
	{
		StringBuffer updatesql = new StringBuffer();
		updatesql.append("update " +storeinfo.getTablename()+" set ");
		Iterator<String> iterator = this.getPropertyNames().iterator();
		Class c ;
		DateTimeFormat activeFormat = DateTimeFormat
		.getFormat("yyyy-M-d H:mm:ss");
		while(iterator.hasNext())
		{
			String key = iterator.next();
			if(key.equals("icode"))
			{
				continue;
			}
				
			if(keyoldvalue.containsKey(key))
			{
				if(get(key)!=null && !(get(key).equals(keyoldvalue.get(key))))
					updatesql.append(""+key+"='"+get(key)+"',");
				
//				if(get(key)==null)
//				{
//					updatesql.append(""+key+"=null,");
//				}
//				else if(!(get(key).equals(keyoldvalue.get(key))))
//				{
//					updatesql.append(""+key+"='"+get(key)+"',");
//				}
				
			}
			else 
			{
				/**
				 * 此处专门负责储存的值是modeldata类型诸如 combox 
				 */
				if(get(key) instanceof ModelData)
				{
					Object o=((ModelData)get(key)).get("code");
					if(o==null)
					{
					updatesql.append(""+key+"=null,");
					}
					else
					{
						updatesql.append(""+key+"='"+o+"',");
					}
				}
				else
				{
					if(get(key)==null)
					{
						updatesql.append(""+key+"=null,");
					}
					else
					{
						c = get(key).getClass();
						//Debug.printf("key = "+key +" value = "+ get(key) +" class =" + get(key).getClass(), 4);
						if(c.equals(Integer.class))
						{
							updatesql.append(""+key+"="+get(key)+",");
						}
						else if(c.equals(Long.class))
						{
							updatesql.append(""+key+"="+get(key)+",");
						}
						else if(c.equals(Float.class))
						{
							updatesql.append(""+key+"="+get(key)+",");
						}
						else if(c.equals(Date.class))
						{
							if(get(key)!=null)
							{
								
								updatesql.append(""+key+"='"+activeFormat.format((Date)get(key))+"',");
							}
							
							else
								updatesql.append(""+key+"=null ,");
							
						}
						else if(c.equals(Double.class))
						{
							updatesql.append(""+key+"="+get(key)+",");
						}
						else if(c.equals(Boolean.class))
						{
							if(((Boolean)get(key))==true)
							updatesql.append(""+key+"=1,");
							else
								updatesql.append(""+key+"=0,");
						}
						else
						{
							updatesql.append(""+key+"='"+get(key)+"',");
						}
						
					//	System.out.println(get(key).getClass());
						//Debug.printf("key="+key +"  class = "+ get(key).getClass(), 3);
						
					//	updatesql.append(""+key+"='"+get(key)+"',");
					}
				}
			}
			
			
		}
		updatesql = updatesql.replace(updatesql.length()-1, updatesql.length(), "");
		
		updatesql.append(" where ");
		Iterator<String> keyiterator = keyoldvalue.keySet().iterator();
		while(keyiterator.hasNext())
		{
			String key = keyiterator.next();
			Object value = getKeyOldValue(key);
			
			updatesql.append("  "+key+"='"+keyoldvalue.get(key)+"' and");
		}
		//去掉最后的 and
		updatesql = updatesql.replace(updatesql.length()-3, updatesql.length(), "");
		
		//updatesql +=" where icode='"+getIcode()+"'";
		GWT.log(""+updatesql, null);
		//Window.alert("updatesql="+updatesql+" \n " +this.getPropertyNames());
		return new String(updatesql);
	}
	/**
	 * 删除sql
	 */
	public String delete()
	{
		if(keyoldvalue.size()==0)
		{
			return null;
		}
		StringBuffer deletesql = new StringBuffer();
		deletesql.append("delete "+storeinfo.getTablename()+ " where ");
		Iterator<String> keyiterator = keyoldvalue.keySet().iterator();
		while(keyiterator.hasNext())
		{
			String key = keyiterator.next();
			Object value = getKeyOldValue(key);
			if(value==null)
				deletesql.append("  "+key+" is null  and");
			else
			deletesql.append("  "+key+"='"+value+"' and");
		}
		//去掉最后的 and
		deletesql = deletesql.replace(deletesql.length()-3, deletesql.length(), "");
		
		//GWT.log(deletesql, null);
		return new String(deletesql);
	}
	/**
	 * @return the storeinfo
	 */
	public StoreInfo getStoreinfo() {
		return storeinfo;
	}
	/**
	 * @param storeinfo the storeinfo to set
	 */
	public void setStoreinfo(StoreInfo storeinfo) {
		this.storeinfo = storeinfo;
	}
	/**
	 * @return the keyoldvalue
	 */
	public java.util.Map<String, Object> getKeyoldvalue() {
		return keyoldvalue;
	}
	/**
	 * @param keyoldvalue the keyoldvalue to set
	 */
	public void setKeyoldvalue(java.util.Map<String, Object> keyoldvalue) {
		this.keyoldvalue = keyoldvalue;
	}

	/**
	 * 设置主键原始值
	 * @param key
	 * @param value
	 */
	public void put(String key,Object value)
	{
		keyoldvalue.put(key, value);
		
	}
	/**
	 * 获得某个主键的原始值
	 * @param key
	 * @return
	 */
	
	public Object getKeyOldValue(String key)
	{
		if(keyoldvalue.containsKey(key))
		return keyoldvalue.get(key);
		else
			return null;
	}

}
